package com.jeespring.security.service.impl;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @description UserServcie
 * @author 张朝阳
 * @date 2021/8/27 0:49
 */
@Service("userDetailService")
public class UserServcie implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String[] list = {"role"};
        if (username.equals("2")){
            list[0] = ("admin");
        }
        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(list);
        return new User(username, new BCryptPasswordEncoder().encode(username), authorityList);
    }
}
